#include <iostream>
#include <fstream>
#include <cstdlib>
#include <csignal>
#include "sysError.h"

using namespace std;

static void sysDumpCrash( )
{
  #if Q_OS_LINUX
  const char * GDB_BATCH_FILE = "bat.data";
  ofstream fout(GDB_BATCH_FILE);
  fout << "where" << endl;
  fout << "detach" << endl;
  fout << "quit" << endl;
  fout.close();

  char buffer[BUFSIZ];
  sprintf(buffer, "gdb attach %d -x=%s -batch > coredump", (int)getpid(), GDB_BATCH_FILE);
  //cout << buffer <<endl;
  system(buffer);
  system("cat coredump");
  #endif
}

static void _SIGSEGV(int sig)
{
  if (SIGSEGV == sig)
  {
    sysDumpCrash();
    abort();
  }
}

void sysRegSigHandle()
{
  signal(SIGSEGV, _SIGSEGV);
}
